<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class album_mod extends module
{
    function __construct()
    {
        $this->uid = @$_SESSION['user']['uid'];
        $this->spid = @$_SESSION['shop']['spid'];
        parent::__construct();
    }
    function upload($save_path, $utype = '0', $new_name = null)
    {
        $result = array();
        $this->save_path = $save_path;
        $this->utype = $utype;
        $this->new_name = $new_name;
        $file_id = array_keys($_FILES);
        $file_id = $file_id[0];
        $fvalue = array_values($_FILES);
        $file_name = $fvalue[0]['name'];
        $file_type = $fvalue[0]['type'];
        $file_size = $fvalue[0]['size'];
        $tmp_name = $fvalue[0]['tmp_name'];
        $result = $this->upload_function($file_id, $file_name, $file_type, $file_size, $tmp_name);
        if ($result['error'] == 0) {
            return $result;
        } else {
            sys_log($result['msg']);
            return $result;
        }
    }
    function upload_function($file_id, $file_name, $file_type, $file_size, $tmp_name)
    {
        if ($file_name) {
            if (in_array($file_type, array('image/jpeg', 'image/gif', 'image/png', 'jpg', 'gif', 'png'))) {
                if ($file_size < 1024 * 1024 * 2) {
                    if (empty($this->new_name)) {
                        $new_name = 'data/' . $this->save_path . '/' . $this->reset_name($file_name);
                    } else {
                        $new_name = $this->new_name;
                    }
                    $row = $this->db->get_one("select * from " . table('api_oss') . " where status = 1 limit 1");
                    if ($row['id'] && $row['status'] == '1') {
                        $alias = $row['alias'];
                        if ($alias == 'local') {
                            $save_path = UQCMS_PATH . $new_name;
                            file::mk_dir($save_path, true);
                            if (move_uploaded_file($tmp_name, $save_path)) {
                                $result['error'] = '0';
                                $result['name'] = $new_name;
                                $result['size'] = filesize($new_name);
                                $result['url'] = $row['host'] . $new_name;
                            } else {
                                $result['error'] = '1';
                                $result['msg'] = '文件移动错误';
                            }
                        } else {
                            $oss_name = 'oss_' . $alias;
                            $param = unserialize($row['pcon']);
                            $os = new $oss_name($param, $row['bucket'], $row['host']);
                            $result = $os->upload($tmp_name, $new_name);
                        }
                        if ($result['error'] == '0') {
                            $insert_id = $this->space_size($result['name'], $result['size'], $this->utype);
                            if ($insert_id) {
                                $data['error'] = '0';
                                $data['id'] = $insert_id;
                                $data['name'] = $result['name'];
                                $data['url'] = $result['url'];
                                if (isset($file_id)) {
                                    $data[$file_id] = $result['url'];
                                }
                                return $data;
                            } else {
                                return array('error' => '1', 'msg' => '录入系统错误');
                            }
                        } else {
                            return array('error' => '1', 'msg' => $result['msg']);
                        }
                    } else {
                        return array('error' => '1', 'msg' => '上传选项错误');
                    }
                } else {
                    return array('error' => '1', 'msg' => '上传文件过大,当前图片' . formatBytes($file_size));
                }
            } else {
                return array('error' => '1', 'msg' => '不支持此类型的文件,支持：jpg,gif,png');
            }
        } else {
            return array('error' => '1', 'msg' => '上传的文件不能为空');
        }
    }
    function upload_move($src_file, $save_path = 'temp', $utype = '0', $new_name = null, $shop_id = '0')
    {
        $this->save_path = $save_path;
        $result = array();
        $row = $this->db->get_one("select * from " . table('api_oss') . " where status = 1 limit 1");
        if ($row['id'] && $row['status'] == '1') {
            if (empty($new_name)) {
                $dst_file = 'data/' . $save_path . '/' . $this->reset_name($src_file);
            } else {
                $dst_file = $new_name;
            }
            $alias = $row['alias'];
            if ($alias == 'local') {
                file::mk_dir($dst_file, true);
                if (rename($src_file, $dst_file)) {
                    $result['error'] = '0';
                    $result['size'] = filesize($dst_file);
                    $result['name'] = $dst_file;
                    $result['url'] = $dst_file;
                } else {
                    $result['error'] = '1';
                    $result['msg'] = '上传错误';
                }
            } else {
                $oss_name = 'oss_' . $alias;
                $param = unserialize($row['pcon']);
                $os = new $oss_name($param, $row['bucket'], $row['host']);
                $result = $os->upload($src_file, $dst_file);
            }
            if ($result['error'] == '0') {
                if (empty($new_name)) {
                    $insert_id = $this->space_size($result['name'], $result['size'], $utype, $shop_id);
                    if ($insert_id) {
                        $result['id'] = $insert_id;
                        return $result;
                    }
                } else {
                    return $result;
                }
            } else {
                return array('error' => '1', 'msg' => $result['msg']);
            }
        } else {
            return array('error' => '1', 'msg' => '没有选择图片');
        }
    }
    function del($file_name)
    {
        $row = $this->db->get_one("select * from " . table('api_oss') . " where status = 1 limit 1");
        if ($row['id'] && $row['status'] == '1') {
            $query = $this->db->delete(table('album'), "url = '" . $file_name . "'");
            if ($query) {
                $alias = $row['alias'];
                if ($alias == 'local') {
                    if (@unlink($file_name)) {
                        return true;
                    } else {
                        return false;
                    }
                } else {
                    $oss_name = 'oss_' . $alias;
                    $param = unserialize($row['pcon']);
                    $os = new $oss_name($param, $row['bucket'], $row['host']);
                    $result = $os->del($file_name);
                    if ($result) {
                        return true;
                    } else {
                        return false;
                    }
                }
            } else {
                return false;
            }
        }
    }
    function upload_tmp($save_path)
    {
        $result = upload::store('temp/images/' . $save_path);
        return json_encode($result);
    }
    function upload_local($save_path)
    {
        $result = upload::store($save_path);
        if ($result['error'] == '0') {
            echo json_encode($result);
        } else {
            exit('error');
        }
    }
    function space_size($file, $size = '0', $utype = '0', $shop_id = '0')
    {
        $setsql['uid'] = isset($this->uid) ? $this->uid : '0';
        if ($this->spid) {
            $setsql['shop_id'] = $this->spid;
        } else {
            $setsql['shop_id'] = $shop_id;
        }
        $path_type = $this->path_type();
        $setsql['type'] = $path_type;
        $setsql['utype'] = $utype;
        $setsql['url'] = $file;
        $setsql['size'] = $size;
        $setsql['up'] = '0';
        $setsql['addtime'] = time();
        $query = $this->db->add(table('album'), $setsql);
        if ($query) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }
    function reset_name($file_name)
    {
        return date("y") . '/' . date("md") . '/' . get_rand(3) . microtime() * 1000000 . date("his", time()) . '.' . $this->getExt($file_name);
    }
    function getExt($path)
    {
        $path = pathinfo($path);
        return strtolower($path['extension']);
    }
    function path_type()
    {
        $arr = $this->path_type_arr();
        $path = $this->save_path;
        if (isset($arr[$path])) {
            return $arr[$path];
        } else {
            return '0';
        }
    }
    function path_type_arr()
    {
        return array('goods' => '1', 'shop' => '2', 'avatar' => '3', 'spd' => '4', 'brand' => '5', 'secret' => '6', 'news' => '7', 'article' => '8', 'images' => '9', 'sucai' => '10', 'comment' => '11', 'refund' => '12', 'static' => '13', 'temp' => '0');
    }
    function path_type_cn()
    {
        return array('1' => '产品图片', '2' => '店铺LOGO', '3' => '用户头像', '4' => '广告图片', '5' => '商标品牌', '6' => '对外保密', '7' => '新闻图片', '8' => '文章图片', '9' => '其他图片', '10' => '图片素材', '11' => '评论图片', '12' => '退款凭证', '13' => '公共文件', '0' => '无效图片');
    }
    function img_replace($content)
    {
        if ($content) {
            preg_match_all('#<img.*?src="([^"]*)"[^>]*>#i', $content, $matches);
            if (!empty($matches)) {
                $imgurl = $matches[1];
                foreach ($imgurl as $val) {
                    $res = explode("data", $val);
                    if (!empty($res[1])) {
                        $new_img_url = 'data' . $res[1];
                        if (strpos($new_img_url, '?')) {
                            $new_img_url = substr($new_img_url, 0, strpos($new_img_url, '?'));
                        }
                        $content = str_replace($val, $new_img_url, $content);
                        unset($res);
                    } else {
                        $content = str_replace($val, $val, $content);
                        unset($res);
                    }
                }
                return $content;
            } else {
                return;
            }
        } else {
            return;
        }
    }
    function img_content($content)
    {
        $img_host = img_host();
        $img_size = img_size('950x0');
        preg_match_all('#<img.*?src="([^"]*)"[^>]*>#i', $content, $matches);
        if (!empty($matches)) {
            $imgurl = $matches[1];
            if (isset($imgurl)) {
                foreach ($imgurl as $val) {
                    if ($val) {
                        $res = explode("data", $val);
                        if (isset($res[1])) {
                            $new_img_url = $img_host . 'data' . $res[1] . $img_size;
                            $content = str_replace($val, $new_img_url, $content);
                            unset($res);
                        } else {
                            $content = str_replace($val, $val, $content);
                            unset($res);
                        }
                    }
                }
            }
        }
        return $content;
    }
}